package spring.boot.jwt.common;


/**
 * 接口返回通用封装
 */
public class Results<T> {
    /**
     * 成功code
     */
    public static final int SUCCESS_CODE = 0;

    /**
     * 参数错误 state
     */
    private static final int PARAM_ERROR_CODE = 100;

    /**
     * 未知错误 state
     */
    private static final int UNKNOWN_ERROR_CODE = 500;

    /**
     * token为空 state
     */
    public static final int TOKEN_ERROR = 405;


    /**
     * 成功 message
     */
    public static final String SUCCESS_MSG = "SUCCESS";
    /**
     * 位置错误 message
     */
    private static final String UNKNOWN_ERROR = "UNKNOWN_ERROR";


    private int state;
    private long time;
    private String message;
    private T content;

    public Results() {
    }


    public Results(int state, String message, T content) {
        this.state = state;
        this.message = message;
        this.content = content;
        this.time = System.currentTimeMillis();
    }

    /**
     * 默认成功结果
     */
    public static Results success() {
        Results results = new Results();
        results.setState(SUCCESS_CODE);
        results.setMessage(SUCCESS_MSG);
        results.setTime(System.currentTimeMillis());
        return results;
    }

    /**
     * 默认成功结果
     */
    public static Results success(Object content) {
        Results results = new Results();
        results.setState(SUCCESS_CODE);
        results.setMessage(SUCCESS_MSG);
        results.setContent(content);
        results.setTime(System.currentTimeMillis());
        return results;
    }

    /**
     * 默认未知错误结果
     */
    public static Results error() {
        Results results = new Results();
        results.setState(UNKNOWN_ERROR_CODE);
        results.setMessage(UNKNOWN_ERROR);
        results.time = System.currentTimeMillis();
        return results;
    }

    /**
     * 未知错误结果  自定义消息
     */
    public static Results error(String message) {
        Results results = new Results();
        results.setState(UNKNOWN_ERROR_CODE);
        results.setMessage(message);
        results.time = System.currentTimeMillis();
        return results;
    }


    /**
     * 参数错误结果
     */
    public static Results paramError(String msg) {
        Results results = new Results();
        results.setState(PARAM_ERROR_CODE);
        results.setMessage(msg);
        results.time = System.currentTimeMillis();
        return results;
    }

    public static Results loginError() {
        Results results = new Results();
        results.setState(UNKNOWN_ERROR_CODE);
        results.setMessage("登录失败");
        results.time = System.currentTimeMillis();
        return results;
    }

    public static Results tokenError() {
        Results results = new Results();
        results.setState(TOKEN_ERROR);
        results.setMessage("token 无效");
        results.time = System.currentTimeMillis();
        return results;
    }


    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }
}
